from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn
import config as cf

PATH_INPUT_DOCX='demo.docx'
PATH_OUTPUT_DOCX='demo_covent'

#输入word文档
path=input('请输入word文档地址,[回车键]输入默认值：demo.docx\n')
if path=='':
    print('已读取>>>demo.docx文档\n')
    doc=Document(PATH_INPUT_DOCX)
else:
    print('已读取>>>%s文档\n'%path)
    doc = Document(path)

#默认样式
doc.styles['Normal'].font.name = cf.FontName
doc.styles['Normal'].font.size = Pt(cf.PT)
doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), cf.FontName)

#str存放读取文本
str=[]
p=doc.paragraphs

#修改字体样式
def add_p(a,pt=cf.PT,BoldTrueFalse=cf.BoldTrueFalse,FontName=cf.FontName):
    a.font.size = Pt(pt)
    a.bold=BoldTrueFalse
    a.font.name = FontName
    a._element.rPr.rFonts.set(qn('w:eastAsia'), FontName)

for x in p:
    str.append(x.text)
    #图片值为None
    if x.text:
        x.text=''
        add_p(x.add_run(str[-1]),pt=16,BoldTrueFalse=True,FontName='幼圆')


#输出word文档
if path=='':
    doc.save('demo_convent.docx')
    print('已生成>>>demo_convent.docx文档\n')
else:
    path_convent=path.rstrip('.docx')+'_convent.docx'
    doc.save(path_convent)
    print('已生成文档>>>%s\n'%path_convent)